package com.visikard.ui.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
import android.widget.TextView;

import com.visikard.item.CouponBuilderItem;
import com.visikard.ui.R;
import com.visikard.ui.activity.CouponBuilderActivityV3;
import com.visikard.ui.activity.MerchantHomeActivity;
import com.visikard.ui.activity.MerchantHomeActivity.IDeleteCoupon;
import com.visikard.utility.BitmapUtils;
import com.visikard.utility.UILImageUtils;
import com.visikard.widget.CouponListItemView;

@SuppressWarnings("deprecation")
public class ListCouponAdapter extends BaseAdapter implements Filterable {

	private Context mContext;
	private ArrayList<CouponBuilderItem> mDataAll;
	private ArrayList<CouponBuilderItem> mData;

	private IDeleteCoupon mCallback;

	public ListCouponAdapter(Context context, ArrayList<CouponBuilderItem> data, IDeleteCoupon callback) {
		// TODO Auto-generated constructor stub
		this.mContext = context;
		this.mDataAll = data;
		this.mData = data;
		this.mCallback = callback;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mData.size();
	}

	@Override
	public Object getItem(int positon) {
		// TODO Auto-generated method stub
		return mData.get(positon);
	}

	@Override
	public long getItemId(int positon) {
		// TODO Auto-generated method stub
		return positon;
	}

	@SuppressWarnings("deprecation")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View datarow = convertView;
		if (datarow == null) {
			datarow = new CouponListItemView(mContext);

		}
		ImageView imageView_coupon = ((CouponListItemView) datarow).mImageView_coupon;
		TextView mTextView_name = ((CouponListItemView) datarow).mTextView_name;
		TextView mTextView_date = ((CouponListItemView) datarow).mTextView_expiration_date;
		SlidingDrawer mDrawer = ((CouponListItemView) datarow).mSlidingDrawer;
		Button mButton_delete = ((CouponListItemView) datarow).mButton_delete;
		Button mButton_edit = ((CouponListItemView) datarow).mButton_edit;

		ImageView imageView_arrow = ((CouponListItemView) datarow).mImageView_arrow;

		TextView mTextView_small_name = ((CouponListItemView) datarow).mTextView_small_name;
		TextView mTextView_small_percentoff = ((CouponListItemView) datarow).mTextView_percentoff;
		TextView mTextView_small_expire = ((CouponListItemView) datarow).mTextView_expiredate;

		int pos = ((CouponListItemView) datarow).position;

		mDrawer.setOnDrawerCloseListener(new MyOnDrawListenerClass(position, (CouponListItemView) datarow));
		mDrawer.setOnDrawerOpenListener(new MyOnDrawListenerClass(position, (CouponListItemView) datarow));

		if (mData != null) {
			CouponBuilderItem item = mData.get(position);
			mTextView_name.setText(item.getCouponName() + " " + item.getPercentOff() + "% Off");
			mTextView_date.setText(item.getExpireDate());

			mTextView_small_name.setText(item.getCouponName());
			mTextView_small_percentoff.setText(item.getPercentOff() + "% OFF");
			mTextView_small_expire.setText(item.getExpireDate());

			UILImageUtils.loadImage(imageView_coupon, item.getImage(), 0, BitmapUtils.ROUND_NONE, 0, 0, 0, 0,
					R.drawable.coupon_default);
			mDrawer.close();
		}
		else {
			mDrawer.close();
		}

		mButton_delete.setOnClickListener(new MyOnClickListenerClass(position, (CouponListItemView) datarow, mData
				.get(position)));
		mButton_edit.setOnClickListener(new MyOnClickListenerClass(position, (CouponListItemView) datarow, mData
				.get(position)));

		new AsynTaskClass((CouponListItemView) datarow, pos).execute((CouponListItemView) datarow);
		return datarow;

	}

	private class AsynTaskClass extends AsyncTask<CouponListItemView, Void, CouponListItemView> {
		private CouponListItemView view;
		private int pos;

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			view.setVisibility(View.INVISIBLE);
		}

		public AsynTaskClass(CouponListItemView v, int p) {
			// TODO Auto-generated constructor stub
			this.view = v;
			this.pos = p;
		}

		@Override
		protected CouponListItemView doInBackground(CouponListItemView... params) {
			// TODO Auto-generated method stub
			view = params[0];
			return view;
		}

		@Override
		protected void onPostExecute(CouponListItemView result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (view.position == pos) {
				result.setVisibility(View.VISIBLE);

			}
		}
	}

	private class MyOnClickListenerClass implements OnClickListener {
		private int position;
		private CouponListItemView couponListItemView;
		private CouponBuilderItem item;

		public MyOnClickListenerClass(int pos, CouponListItemView datarow, CouponBuilderItem i) {
			// TODO Auto-generated constructor stub
			this.position = pos;
			this.couponListItemView = datarow;
			this.item = i;
		}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub

			if (v == couponListItemView.mButton_edit) {

				Intent intent = new Intent(mContext, CouponBuilderActivityV3.class);
				mContext.startActivity(intent);
			}
			else if (v == couponListItemView.mButton_delete) {
				((MerchantHomeActivity) mContext).showConfirmDeleteDialog(mCallback, item);
			}

		}
	}

	@SuppressWarnings("deprecation")
	private class MyOnDrawListenerClass implements OnDrawerCloseListener, OnDrawerOpenListener {

		private int position;
		private CouponListItemView couponListItemView;

		public MyOnDrawListenerClass(int pos, CouponListItemView datarow) {
			// TODO Auto-generated constructor stub
			this.position = pos;
			this.couponListItemView = datarow;

		}

		@Override
		public void onDrawerOpened() {
			// TODO Auto-generated method stub
			couponListItemView.mImageView_arrow.setImageResource(R.drawable.icon_row_right);
		}

		@Override
		public void onDrawerClosed() {
			// TODO Auto-generated method stub
			couponListItemView.mImageView_arrow.setImageResource(R.drawable.icon_row_left);
		}

	}

	Filter filter = new Filter() {

		@SuppressWarnings("unchecked")
		@Override
		protected void publishResults(CharSequence constraint, FilterResults results) {
			mData = (ArrayList<CouponBuilderItem>) results.values;
			notifyDataSetChanged();
		}

		@SuppressLint("DefaultLocale")
		@Override
		protected FilterResults performFiltering(CharSequence constraint) {
			FilterResults result = new FilterResults();

			ArrayList<CouponBuilderItem> filterList = new ArrayList<CouponBuilderItem>();
			if (constraint.length() > 0) {
				for (CouponBuilderItem item : mDataAll) {
					if (item.getCouponName().toLowerCase().contains(constraint.toString().toLowerCase())) {
						filterList.add(item);
					}
				}
				result.values = filterList;
				return result;
			}

			result.values = mDataAll;
			return result;
		}
	};

	@Override
	public Filter getFilter() {
		// TODO Auto-generated method stub
		return filter;
	}

}
